<!DOCTYPE html>
<!--
/*
** Copyright (c) 2014 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
 -->
<html>
<head>
<title>J3DIMath unit test</title>
<script src="J3DIMath.js"></script>
<script>
var log = function(msg) {
    var p = document.createElement('p');
    if (msg.indexOf('FAIL') == 0) {
        p.style.color = '#900';
    } else if (msg.indexOf('PASS') == 0) {
        p.style.color = '#090';
    }
    p.textContent = msg;
    document.body.appendChild(p);
    console.log(msg);
};

var check = function(result, msg) {
    if (result) {
        log('PASS ' + msg);
    } else {
        log('FAIL ' + msg);
    }
};

var testMultiplication = function() {
    var A = new J3DIMatrix4();
    var B = new J3DIMatrix4();
    A.$matrix.m21 = 3; // 2nd column, 1st row
    B.$matrix.m12 = 2; // 1st column, 2nd row
    A.multiply(B); // multiply from the right
    // [ 1 3 0 0 ]   [ 1 0 0 0 ]   [ 7 3 0 0 ]
    // | 0 1 0 0 | x | 2 1 0 0 | = | 2 1 0 0 |
    // | 0 0 1 0 |   | 0 0 1 0 |   | 0 0 1 0 |
    // [ 0 0 0 1 ]   [ 0 0 0 1 ]   [ 0 0 0 1 ]
    check(A.$matrix.m11 == 7 && A.$matrix.m12 == 2 && A.$matrix.m21 == 3 && A.$matrix.m22 == 1,
          'Matrix multiplication order is as specified');
};

var testFloatArrayIO = function() {
    // Column major order
    var array = [ 7, 2, 0, 0,
                  3, 1, 0, 0,
                  0, 0, 1, 0,
                  0, 0, 0, 1 ];
    var A = new J3DIMatrix4(array);
    check(A.$matrix.m11 == 7 && A.$matrix.m12 == 2 && A.$matrix.m21 == 3 && A.$matrix.m22 == 1,
          'Matrix can be constructed from column-major array');
    var outArray = A.getAsArray();
    var match = true;
    for (var i = 0; i < array.length; ++i) {
        match = match && array[i] == outArray[i];
    }
    check(match, 'Matrix array out matches array in');
};

var testCrossProduct = function() {
    var a = new J3DIVector3(1, 5, 11);
    var b = new J3DIVector3(3, 7, 13);
    a.cross(b);
    var arr = a.getAsArray();
    check(arr[0] == 5 * 13 - 11 * 7, 'Vector cross product x is correct');
    check(arr[1] == 11 * 3 - 1 * 13, 'Vector cross product y is correct');
    check(arr[2] == 1 * 7 - 5 * 3, 'Vector cross product z is correct');
};

var testLinearAlgebra = function(useCSSMatrix) {
    J3DIHasCSSMatrix = useCSSMatrix;
    log('Using CSSMatrix: ' + J3DIHasCSSMatrix);

    testMultiplication();
    testFloatArrayIO();
    testCrossProduct();
};

var test = function() {
    testLinearAlgebra(false);
    testLinearAlgebra(true);
};
</script>
</head>
<body onload="test()">
</body>
</html>
